package com.showde.shoppingmall.adapter;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.activity.AddressEditActivity;
import com.showde.shoppingmall.activity.MoreGroup;
import com.showde.shoppingmall.business.logic.AddressLogic;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.AddressDBCommand;
import com.showde.shoppingmall.domain.Address;
import com.showde.shoppingmall.utils.ActivityDispatch;
import com.showde.shoppingmall.utils.ToastUtils;

public class AddressItemAdapter extends BaseAdapter {

	private static final int MSG_DELETEADDRESS_SUCCESS = 0x10001;
	private static final int MSG_DELETEADDRESS_FAIL = 0x10002;

	private Context context;

	private List<Address> addresses;

	private AddressDBCommand addressDBCommand;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_DELETEADDRESS_SUCCESS:
				refresh();
				break;
			case MSG_DELETEADDRESS_FAIL:
				ToastUtils.showToast(context, "删除收件地址失败...");
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	public AddressItemAdapter(Context context, List<Address> addresses) {
		this.context = context;
		this.addresses = addresses;

		addressDBCommand = new AddressDBCommand(context);
	}

	public void resetData(List<Address> addresses) {
		this.addresses = addresses;
	}

	@Override
	public int getCount() {
		return addresses.size();
	}

	@Override
	public Address getItem(int position) {
		return addresses.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_address, null);
			holder = new ViewHolder();

			holder.name = (TextView) convertView.findViewById(R.id.txt_name);

			holder.addressContent = (TextView) convertView
					.findViewById(R.id.txt_address);

			holder.modify = (TextView) convertView
					.findViewById(R.id.txt_modify);
			holder.delete = (TextView) convertView
					.findViewById(R.id.txt_delete);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Address address = addresses.get(position);

		final String pk = address.getRecipientId();

		holder.name.setText(address.getRecipient());

		holder.addressContent.setText(address.getAddressContent());

		holder.modify.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// to edit page
				Bundle bundle = new Bundle();
				bundle.putSerializable(ActivityDispatch.ADDRESS_KEY, address);
				ActivityDispatch.openNewActivity(context,
						AddressEditActivity.class, MoreGroup.group,
						"ProductDetailsActivity", bundle,
						ActivityDispatch.GROUP_MORE);
			}
		});

		holder.delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				AddressLogic.deleteAddress(context, pk,
						new UICallbackInterface() {

							@Override
							public void onSuccess(CallbackData data) {
								handler.sendEmptyMessage(MSG_DELETEADDRESS_SUCCESS);
							}

							@Override
							public void onError(String errorMsg) {
								handler.sendEmptyMessage(MSG_DELETEADDRESS_FAIL);
							}
						});
			}
		});

		return convertView;
	}

	private void refresh() {
		addresses = addressDBCommand.queryAll();
		notifyDataSetChanged();
	}

	private class ViewHolder {
		private TextView name;

		private TextView addressContent;

		private TextView modify;
		private TextView delete;
	}

}
